﻿using System;
using System.Linq;
using System.Windows;
using Shoozla.ViewModel;
using Shoozla.View;
using System.IO;
using Shoozla.DataModel;
using MahApps.Metro.Controls;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : MetroWindow
    {

        public MainView()
        {
            InitializeComponent();
            DataContext = new MainViewModel(this,null);
            //registering for events
            //FolderListViewModel folderModel = (FolderListViewModel)App.FolderView.DataContext;
          //  ((MainViewModel)this.DataContext).FolderAdded += new MainViewModel.FolderAddedDelegate(folderModel_AddFolder);
            this.Closing += new System.ComponentModel.CancelEventHandler(MainView_Closing);
            
        }

        void MainView_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
           // to block in case there are songs to save
            if (AppModel.IsCollectionDirty())
            {
               var decision = MetroMessageBox.MetroShow("", "Do you want to save the collection?", true, this, MessageIcon.Info,true);
               if (decision == MessageBoxResult.OK)
               {
                   e.Cancel = true;
                   ((MainViewModel)App.MainView.DataContext).CmdSave.Execute(null);
               }
            }
        }

       
        private void MetroWindow_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;  
    
        }

        private void MetroWindow_DragLeave(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_DragOver(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_Drop(object sender, DragEventArgs e)
        {
            bool error = false;
            try
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] dir = (string[])e.Data.GetData(DataFormats.FileDrop);
                    if (Directory.Exists(dir[0]))
                    {
                       // mainFE.AddFolderToView(new DirectoryInfo(dir[0]));
                        ((MainViewModel)DataContext).DoCmdOpenFolder(new DirectoryInfo(dir[0]));
                    }
                    else
                        error = true; 
                }
            }
            catch(Exception ex)
            {
                error = true;
               
            }
            finally
            {
                if(error)
                    MetroMessageBox.MetroShow("", "You can drop only one Directory", true, this, MessageIcon.Warning, false);

            }


        }


    }
}
